package com.lysj.order.report.controller;

import cn.hutool.core.date.DateUtil;
import com.lysj.admin.utils.constant.CommonConstant;
import com.lysj.admin.utils.spring.foundation.base.BaseContent;
import com.lysj.admin.utils.web.Resp;
import com.lysj.order.order.domain.Order;
import com.lysj.order.order.domain.OrderItem;
import com.lysj.order.order.service.OrderItemService;
import com.lysj.order.order.service.OrderService;
import com.lysj.order.report.vo.DayBusinessStatis;
import com.lysj.order.report.vo.FinancialManageStatis;
import com.lysj.order.report.vo.FoodReportVo;
import com.lysj.order.report.vo.HandoverReportVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @ Author ：drj.
 * @ Date  ：Created in 10:31 2019/4/9
 * @ Description: 订单报表
 **/
@Slf4j
@RestController
@RequestMapping("/order/report")
public class ReportController extends BaseContent {

    @Resource
    private OrderService orderService;

    @Resource
    private OrderItemService orderItemService;

    /*
     * @author drj
     * @date 2019-03-07 18:22
     * @Description :财务统计
     */
    @GetMapping("/financial_business")
    public Resp financialBusiness(String storeId, @DateTimeFormat(pattern = "yyyy-MM-dd") Date beginDate, @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate) throws ParseException {

        Date beginDate1 = DateUtil.beginOfDay(beginDate);
        Date endDate1 = DateUtil.endOfDay(endDate);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");//Date指定格式：yyyy-MM-dd HH:mm:ss:SSS

        List<Object[]> objectList = orderService.getRepository().daylyStatic(storeId, simpleDateFormat.parse(simpleDateFormat.format(beginDate1)), simpleDateFormat.parse(simpleDateFormat.format(endDate1)));
        List<FinancialManageStatis> financialManageStatisList = new LinkedList<>();
        for (Object[] objects : objectList) {
            FinancialManageStatis financialManageStatis = new FinancialManageStatis();
            financialManageStatis.setDate(String.valueOf(objects[0]));
            financialManageStatis.setOrderNum((BigInteger) objects[1]);
            financialManageStatis.setSumMoney((BigDecimal) objects[2]);
            financialManageStatisList.add(financialManageStatis);
        }
        return new Resp().success(financialManageStatisList);
    }

    /*
     * @author drj
     * @date 2019-04-08 16:03
     * @Description ：日营业统计
     */
    @GetMapping("/day_summary_business")
    public Resp daySummaryBusiness(String storeId, @DateTimeFormat(pattern = "yyyy-MM-dd") Date beginDate, @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate) throws ParseException {
        Date beginDate1 = DateUtil.beginOfDay(beginDate);
        Date endDate1 = DateUtil.endOfDay(endDate);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");//Date指定格式：yyyy-MM-dd HH:mm:ss:SSS

        List<Object[]> objectList = orderService.getRepository().businessStatic(storeId, simpleDateFormat.parse(simpleDateFormat.format(beginDate1)), simpleDateFormat.parse(simpleDateFormat.format(endDate1)));
        List<DayBusinessStatis> dayBusinessStatisList = new LinkedList<>();
        for (Object[] objects : objectList) {
            DayBusinessStatis dayBusinessStatis = new DayBusinessStatis();
            dayBusinessStatis.setDate(String.valueOf(objects[0]));
            dayBusinessStatis.setOrderAmount((BigInteger) objects[1]);
            dayBusinessStatis.setCusAmount((BigDecimal) objects[2]);
            dayBusinessStatis.setDisCountMoney((BigDecimal) objects[3]);
            dayBusinessStatis.setActPayMoney((BigDecimal) objects[4]);
            dayBusinessStatisList.add(dayBusinessStatis);
        }
        return new Resp().success(dayBusinessStatisList);
    }


    /*
     * @author drj
     * @date 2019-04-08 11:41
     * @Description ：，门店总收入跟订单总数统计
     */
    @GetMapping("/ordernum_and_money")
    public Resp orderNumAndMoney(String storeId) {
        //订单数量
        Integer orderNum = orderService.getRepository().countByStoreIdAndStatusAndDelFlag(storeId, Order.Status.SUCCESSPAY.getCode(), CommonConstant.NORMAL_FLAG);
        //现金支付
        BigDecimal cash = orderService.getRepository().summaryMoney(storeId, Order.PayWay.CASH.getCode());
        //在线支付
        BigDecimal onlineMoney = orderService.getRepository().summaryMoney(storeId, Order.PayWay.WECHAT.getCode());
        Map<String, Object> map = new HashMap<>();
        map.put("orderNum", orderNum);
        map.put("cash", null == cash ? BigDecimal.ZERO : cash);
        map.put("onlineMoney", null == onlineMoney ? BigDecimal.ZERO : onlineMoney);
        return new Resp().success(map);
    }

    /*
     * @author drj
     * @date 2019-04-09 10:53
     * @Description :赠菜或退菜
     */
    @GetMapping("/gift_food_or_cancle_food")
    public Resp giftFoodOrCancleFood(String storeId, @DateTimeFormat(pattern = "yyyy-MM-dd") Date beginDate, @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate, Integer status) {
        Map<String, FoodReportVo> map = new HashMap<>();
        //获取对应时间的订单
        List<Order> orderList = orderService.getRepository().findByStoreIdAndStatusAndDelFlagAndCreateTimeBetween(storeId, Order.Status.SUCCESSPAY.getCode(), CommonConstant.NORMAL_FLAG, DateUtil.beginOfDay(beginDate), DateUtil.endOfDay(endDate));
        //遍历订单信息
        for (Order order : orderList) {
            List<OrderItem> orderItemList = orderItemService.getRepository().findByOrderIdAndStatusAndDelFlag(order.getId(), status, CommonConstant.NORMAL_FLAG);
            //遍历订单查询子单中的赠菜信息
            for (OrderItem orderItem : orderItemList) {
                if (map.containsKey(orderItem.getFoodId())) {
                    FoodReportVo foodReportVo = (FoodReportVo) map.get(orderItem.getFoodId());
                    foodReportVo.setAmount(map.get(orderItem.getFoodId()).getAmount() + orderItem.getAmount());
                    foodReportVo.setMoney(map.get(orderItem.getFoodId()).getMoney().add(orderItem.getTolPrice()));
                } else {
                    FoodReportVo foodReportVo = new FoodReportVo();
                    foodReportVo.setName(orderItem.getName());
                    foodReportVo.setAmount(orderItem.getAmount());
                    foodReportVo.setMoney(orderItem.getTolPrice());
                    map.put(orderItem.getFoodId(), foodReportVo);
                }
            }
        }
        //将map转为list
        List<FoodReportVo> foodReportVoList = new LinkedList<>();
        for (Object o : map.keySet()) {
            FoodReportVo foodReportVo = (FoodReportVo) map.get(o);
            foodReportVoList.add(foodReportVo);
        }
        return new Resp().success(foodReportVoList);
    }

    /*
     * @author drj
     * @date 2019-04-10 10:33
     * @Description :每日交接
     */
    @GetMapping("/handover")
    public Resp handover(String storeId, @DateTimeFormat(pattern = "yyyy-MM-dd") Date beginDate, @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate) throws ParseException {

        Date beginDate1 = DateUtil.beginOfDay(beginDate);
        Date endDate1 = DateUtil.endOfDay(endDate);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");//Date指定格式：yyyy-MM-dd HH:mm:ss:SSS

        List<Object[]> objectList = orderService.getRepository().handover(storeId, simpleDateFormat.parse(simpleDateFormat.format(beginDate1)), simpleDateFormat.parse(simpleDateFormat.format(endDate1)));
        List<HandoverReportVo> handoverReportVoList = new LinkedList<>();
        for (Object[] objects : objectList) {
            HandoverReportVo handoverReportVo = new HandoverReportVo();
            handoverReportVo.setDate(String.valueOf(objects[0]));
            handoverReportVo.setCash((BigDecimal) objects[1]);
            handoverReportVo.setWechatPay((BigDecimal) objects[2]);
            handoverReportVo.setTolOrderAmount((BigInteger) objects[3]);
            handoverReportVo.setRefundOrderAmount((BigInteger) objects[4]);
            handoverReportVo.setRefundMoney((BigDecimal) objects[5]);
            handoverReportVoList.add(handoverReportVo);
        }
        return new Resp().success(handoverReportVoList);
    }

}
